package com.chookss.home.audit.seal;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.entity.WorkNewEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.MyEvent;
import com.chookss.tools.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SealRecordAdapter extends BaseQuickAdapter<WorkNewEntity, BaseViewHolderEx> {
    private Activity context;

    public SealRecordAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<WorkNewEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, WorkNewEntity entity) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        TextView tvTitleType = helper.getView(R.id.tvTitleType);
        TextView tvTime = helper.getView(R.id.tvTime);
        TextView tvTime2 = helper.getView(R.id.tvTime2);
        TextView tvNum = helper.getView(R.id.tvNum);
        TextView tvRemark = helper.getView(R.id.tvRemark);
        TextView tvRemarkInit = helper.getView(R.id.tvRemarkInit);
        LinearLayout llTime = helper.getView(R.id.llTime);
        LinearLayout llNum = helper.getView(R.id.llNum);
        tvTitle.setText(entity.getUseName());
        tvTime2.setText("发起时间："+entity.getCreateTime());
        if (!Utils.isNull(entity.getUseDetail())) {
            tvRemarkInit.setText("刻章内容：");
            if (Utils.isNull(entity.getUseDetail())) {
                tvRemark.setText("无");
            } else {
                tvRemark.setText(entity.getUseDetail());
            }
            if (Utils.isNull(entity.getUseNum())) {
                llNum.setVisibility(View.GONE);
            } else {
                llNum.setVisibility(View.VISIBLE);
                tvNum.setText("刻章数量：" + ClearMoreZeroUtil.subZeroAndDot(entity.getUseNum()) + "个");
            }
        } else {
            tvRemarkInit.setText("用章事由：");
            if (Utils.isNull(entity.getRemark())) {
                tvRemark.setText("无");
            } else {
                tvRemark.setText(entity.getRemark());
            }
            if (Utils.isNull(entity.getUseTime())) {
                llTime.setVisibility(View.GONE);
            } else {
                llTime.setVisibility(View.VISIBLE);
                tvTime.setText("使用时间：" + entity.getUseTime());
            }
            if (Utils.isNull(entity.getUseNum())) {
                llNum.setVisibility(View.GONE);
            } else {
                llNum.setVisibility(View.VISIBLE);
                tvNum.setText("使用次数：" + ClearMoreZeroUtil.subZeroAndDot(entity.getUseNum()) + "次");
            }
        }
        if (entity.getCacheStatus() == 2) {
            tvTitleType.setText("上传失败");
            tvTitleType.setTextColor(context.getResources().getColor(R.color.main_color));
            tvTitleType.setBackgroundResource(R.drawable.shape_main_color_4_1f);
        } else {

            if ("0".equals(entity.getAuditCd())) {
                tvTitleType.setText("审批中");
                tvTitleType.setTextColor(Color.parseColor("#FCA817"));
                tvTitleType.setBackgroundResource(R.drawable.shape_fca817_4_1f);
            } else if ("1".equals(entity.getAuditCd())) {
                tvTitleType.setText("已通过");
                tvTitleType.setTextColor(Color.parseColor("#44D3B3"));
                tvTitleType.setBackgroundResource(R.drawable.shape_44d3b3_4_2f);
            } else {
                if ("3".equals(entity.getAuditCd())) {
                    tvTitleType.setText("废弃");
                } else {
                    tvTitleType.setText("不通过");
                }
                tvTitleType.setTextColor(Color.parseColor("#F2434D"));
                tvTitleType.setBackgroundResource(R.drawable.shape_f2434d_4_2f);
            }
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Urls.antiShake.check()) {
                    return;
                }
                Intent intent = null;
                if (entity.getCacheStatus() == 2) {
                    //上传失败
                    Map<String, String> map = new HashMap<>();
                    map.put("ClockInAllRecordActivity_error_position", helper.getItemPosition() + "");
                    EventBus.getDefault().post(new MyEvent(map));
                } else {
                    intent = new Intent(context, SealDetailActivity.class);
                    entity.setProjectCode(entity.getUseCode());
                    intent.putExtra("data", entity);
                    intent.putExtra("type", Utils.changeNumber(entity.getWorkFlowType()));
                    context.startActivity(intent);
                }
            }
        });
    }
}
